﻿using Sustainalytics.Synchronization.Interface;
using System;

namespace Sustainalytics.CRT.Synchronization
{
    public class FocusedRatingsComputationSyncStep2 : ISyncStep
    {
        public SyncStepOrder ExecutionOrder => SyncStepOrder.FocusedRatingsComputationOutput2;

        public SyncContext Start(SyncContext syncContext)
        {
            syncContext.Token.ThrowIfCancellationRequested();
            syncContext.Log("Begin sync focused ratings computation");

            if (!syncContext.GAConnectionStrings.ContainsKey("ClientDW"))
                throw new ArgumentException("ClientDW key is missing!");

            if (!syncContext.GAConnectionStrings.ContainsKey("ClientPlatform"))
                throw new ArgumentException("ClientPlatform key is missing!");

            try
            {
                var focusedRatingsComputation = new FocusedRatingsComputation();

                focusedRatingsComputation.Compute(syncContext, true).Wait();

                syncContext.Token.ThrowIfCancellationRequested();

                var focusedRatingsKeyEsgIssuesManager = new FocusedRatingsKeyEsgIssuesManager();
                focusedRatingsKeyEsgIssuesManager.Sync(syncContext);

                syncContext.Log("End sync focused ratings computation");
            }
            catch (System.Exception ex)
            {
                syncContext.Log(ex.ToString());
                syncContext.Status = SyncOperationStatus.Failed;
                throw;
            }

            return syncContext;
        }
    }
}
